/***********************************************************************  
 *   @package：cn.wj.base.controller，@class-name：BaseFilter.java   
 *   @copyright     Copyright:   2015-2016     
 *   @creator       wenjie 
 *   @create-time   2016年1月5日   下午10:12:14   
 *   @revision      $Id: 1.0    
 ***********************************************************************/
package cn.wj.base.controller;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.wj.base.util.PropertiesLoader;

/**
 * Servlet Filter implementation class BaseFilter
 */
@WebFilter("/*")
public class BaseFilter implements Filter {
	private static PropertiesLoader propertiesLoader = null;
	private static String[] resources;

	/**
	 * Default constructor.
	 */
	public BaseFilter() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		propertiesLoader = PropertiesLoader.getInstance();
		resources = propertiesLoader.staticResources().split(",");
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		request.setCharacterEncoding("UTF-8");   
		String requestUrl = request.getRequestURI().substring(request.getContextPath().length());
		//将请求纳入管理
		if(requestUrl.endsWith(".do")){
				StringBuffer url = new StringBuffer();
				url.append(requestUrl.substring(0,requestUrl.lastIndexOf("/")));
				url.append("?WJFrameWorkMethod=");
				url.append(requestUrl.substring(requestUrl.lastIndexOf("/")+1,requestUrl.lastIndexOf(".")));
				request.getRequestDispatcher(url.toString()).forward(request, response);
		}else {
			chain.doFilter(request, response);
		}
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

}
